﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RollBack.src.ui
{
    public partial class LogMessageViewer : UserControl
    {
        public LogMessageViewer()
        {
            InitializeComponent();
        }

        public void addLogLine(log4net.Core.LoggingEvent loggingEvent, string msg)
        {
            if (!m_logMessageTextBox.IsDisposed)
            {
                if (this.InvokeRequired)
                {
                    MethodInvoker method = delegate()
                    {
                        this.addLogLineHandler(loggingEvent, msg);
                    };
                    this.Invoke(method);
                }
                else
                {
                    addLogLineHandler(loggingEvent, msg);
                }
            }
        }

        private void addLogLineHandler(log4net.Core.LoggingEvent loggingEvent, string msg)
        {
            if (!m_logMessageTextBox.IsDisposed)
            {
                if (m_logMessageTextBox.Lines.Length > 100000)
                {
                    // clear out lines after a limit
                    m_logMessageTextBox.Clear();
                }
                Color color = Color.FromKnownColor(KnownColor.Black);
                if (loggingEvent.Level.Value == log4net.Core.Level.Info.Value)
                {
                    color = Color.FromKnownColor(KnownColor.Blue);
                }
                else if (loggingEvent.Level == log4net.Core.Level.Error ||
                    loggingEvent.Level == log4net.Core.Level.Fatal ||
                    loggingEvent.Level == log4net.Core.Level.Critical ||
                    loggingEvent.Level == log4net.Core.Level.Alert ||
                    loggingEvent.Level == log4net.Core.Level.Emergency)
                {
                    color = Color.FromKnownColor(KnownColor.Red);
                }
                else if (loggingEvent.Level == log4net.Core.Level.Warn)
                {
                    color = Color.FromKnownColor(KnownColor.Red);
                }
                m_logMessageTextBox.SelectionColor = color;
                m_logMessageTextBox.AppendText(msg);
            }
        }

        internal void clearLogMessages()
        {
            m_logMessageTextBox.Clear();
        }
    }
}
